//
//  ControlProtocol.swift
//  aquariumstudio
//
//  Created by zj on 2022/10/20.
//  Copyright © 2022 LXF. All rights reserved.
//

import Foundation

fileprivate typealias log = XXLoger

public protocol DeviceProtocol:NSObjectProtocol{
    var typeString:String?{
        get
    }
    var deviceId:String?{
        get
    }
    var displayName:String?{
        get
    }
}

public protocol ControlProtocol:NSObjectProtocol {
    
    var cmdName:String {get}
    
    var device:DeviceProtocol? { get set }
    
    func canControl(_ typeString:String)->Bool
    func canControl(_ device:DeviceProtocol)->Bool
    
    func copyControlProtocol()->ControlProtocol
    
    func switchTo( value:Bool)
    
    func setRGB(r:Int,g:Int,b:Int)
    func setRGBW(r:Int,g:Int, b:Int, w:Int)
    
}

open class ControlProtocolImpl:NSObject,ControlProtocol {
    public var cmdSet:CmdSet?
    open var cmdName:String{
        return ""
    }
    
    open func copyControlProtocol() -> ControlProtocol {
        log.e("请实现此方法 %@".xx_format(self.classForCoder.description()))
        return ControlProtocolImpl.init()
    }
    
    open var device: DeviceProtocol?{
        didSet{
            initCmdSet()
            deviceChange(device: device)
        }
    }
    
    open func deviceChange(device: DeviceProtocol?){
        
    }
    
    open func initCmdSet(){
        var cmdName = cmdName.nilOrNotEmptyString
        if cmdName == nil{
            cmdName = self.classForCoder.description().split(separator: ".").last?.description ?? "-not cmdName by - \(self.classForCoder.description())"
        }
        
        cmdSet = CmdSetManager.shared.getCmdSet(name: cmdName ?? "")
        if cmdSet == nil{
            log.e("cmdSet == nil, cmdName = \(cmdName ?? "") , class = \(self.classForCoder.description())")
        }
    }
    
    open func sendCmd(cmd:Cmd?,para:[String:UInt8],interval:Bool = false,intervalFlag:String = ""){
        guard let cmd = cmd else {
            log.e("cmd == nil")
            return  }
        var comp:[String] = []
        
        if cmd.cmd.contains(","){
            comp = cmd.cmd.split(separator: ",").map({$0.description})
        }else{
            comp = cmd.cmd.split(separator: " ").map({$0.description})
        }
        
        
        if cmd.paraIndex == nil {
            cmd.paraIndex = [:]
        }
        
        var cmdU8List = comp.toUInt8List({ index, v in
            if var paraIndex = cmd.paraIndex{
                if paraIndex.index(forKey: v.trim) == nil{
                    paraIndex[v.trim] = index
                    cmd.paraIndex = paraIndex
                }
            }
            return 0
        })
        
        cmdU8List.safe_replece(indexs: cmd.paraIndex ?? [:], values: para)
        
        log.e("[\(cmd.name)] [\(cmd.cmd)] para=\(para)")
        log.d("\(cmdU8List.toHexList())")
        
        sendCmd(data: cmdU8List,interval: interval,intervalFlag:cmd.name)
    }
    
    open func sendCmd(data:[UInt8],interval:Bool = false,intervalFlag:String = ""){
        log.e("请实现此方法 %@".xx_format(self.classForCoder.description()))
    }
    
    open func canControl(_ typeString: String) -> Bool {
        log.e("请实现此方法 %@".xx_format(self.classForCoder.description()))
        return false
    }
    
    open func canControl(_ device: DeviceProtocol) -> Bool {
        log.e("请实现此方法 %@".xx_format(self.classForCoder.description()))
        return false
    }
    
    open func switchTo(value: Bool) {
        log.e("请实现此方法 %@".xx_format(self.classForCoder.description()))
    }
 
    open func setRGB(r: Int, g: Int, b: Int) {
        log.e("请实现此方法 %@".xx_format(self.classForCoder.description()))
    }
    open func setRGBW(r: Int, g: Int, b: Int, w: Int) {
        log.e("请实现此方法 %@".xx_format(self.classForCoder.description()))
    }
}

